// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    phone: '', // 手机号码
    code: '', // 验证码
    sendText: '获取验证码',
    sendCss: 'send',
    timer: null,
    countDown: 60,
    AppCode: 'e3f72f462f5a4e9ea0e5d1ab5ee0eabc',
    url: 'https://gyytz.market.alicloudapi.com/sms/smsSend',
    templateId: '908e94ccf08b4476ba6c876d13f084ad',
    smsSignId: '2e65b1bb3d054466b82f0c9d125465e2',
    param: '**code**:',
    author: 'Authorization',
    userVerificationCode: ''
  },

  sendMsg() {
    if (this.data.sendCss == 'sendVerificationCode') return
    if (!this.checkPhone()) {
      this.popup('手机号码无效')
      return
    }
    this.setData({
      sendText: '获取验证码',
      sendCss: 'sendVerificationCode'
    })
    this.data.timer = setInterval(() => {
      // console.log('定时器的钩子函数被调用了' + this.data.countDown);
      this.setData({
        countDown: this.data.countDown - 1,
        sendText: this.data.countDown + '后重新获取',
      })
      if (this.data.countDown == 0) {
        clearInterval(this.data.timer)
        this.setData({
          countDown: 59,
          sendText: '重新获取',
          sendCss: 'send'
        })
      }
    }, 1000)
    this.sendSMSVerificationCode()
    console.log(this.data.userVerificationCode);
  },
  getPhone(e) {
    this.setData({
      phone: e.detail.value
    })
    // console.log(this.data.phone);
  },
  getCode(e) {
    this.setData({
      code: e.detail.value
    })
    // console.log(this.data.code);
  },
  checkPhone() {
    let phoneReg = /^1[35789]\d{9}$/
    return phoneReg.test(this.data.phone)
  },
  popup(msg) {
    wx.showToast({
      title: msg,
      icon: msg.length > 7 ? 'none' : 'error'
    })
  },
  login() {
    if (!this.checkPhone()) {
      this.popup('手机号码无效')
      return
    }
    // console.log('phone = ' + this.data.phone);
    // console.log('code = ' + this.data.code);
    if(this.data.userVerificationCode != this.data.code) {
      this.popup('验证码不正确')
      return
    }
    this.dologin()
    wx.navigateTo({
      url: '/pages/main/main',
    })
  },
  sendSMSVerificationCode() {
    wx.request({
      url: this.data.url,
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'APPCODE ' + this.data.AppCode
      },
      data: {
        'mobile': this.data.phone,
        'smsSignId': this.data.smsSignId,
        'templateId': this.data.templateId,
        'param': this.data.param + this.randomVerificationCode() + ',**minute**:5'
      },
      success: res => {
        console.log(res);
      },
    })
  },
  randomVerificationCode() {
    let s = ''
    for (let i = 0; i < 6; i++) {
      let n = parseInt(Math.random() * 10)
      s += n
    }
    this.setData({
      userVerificationCode: s
    })
    return s
  },
  dologin() {
    wx.request({
      url: 'http://localhost:8080/users',
      method: 'POST',
      data: {'phone': this.data.phone},
      success(res) {
        console.log(res);
      }
    })
  }
})